package cetcbigdata.da.business_environment.zhejiang;

import cetcbigdata.da.base.Base;
import cetcbigdata.da.service.zhejiang.qitaqingdan.UtilZheJiang;
import cn.wanghaomiao.xpath.model.JXDocument;
import com.alibaba.datax.common.plugin.RecordSender;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class SpiderZheJiang extends Base {
    private static String baseRegion = "浙江省";

    private static String baseModule = "营商环境";

    @Override
    public JSONObject getDataDemo() {
        try {
            return processResult("xxx", "xxx", "xxx", "xxx", new JSONObject(), "xxx", "xxx");
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public void getData(RecordSender recordSender, List<Object> fieldsList) {
        for(JSONObject regionItem : getRegionList()){
            List<JSONObject> topicList = getTopic(regionItem);
            for(JSONObject topicItem : topicList){
                getSubTopic(
                        topicItem.getString("url"),
                        recordSender,
                        fieldsList,
                        baseRegion + "-" + regionItem.getString("name"),
                        baseModule + "-" + topicItem.getString("name")
                );
            }
        }
    }


    private static List<JSONObject> getTopic(JSONObject regionItem){
        String url = "https://www.zjzwfw.gov.cn/zjservice/home/corporate/index.do?classify=5&webId=" + regionItem.getString("id");
        String response = getReq(url, UtilZheJiang.getHeaders(), new JSONObject());
        List<JSONObject> out = new ArrayList<>();
        JXDocument jxDocument = new JXDocument(response);
        List<Object> listObj = jxDocument.sel("//ul[@class='hotNav_list clearfix']/li");
        for(Object o : listObj){
            JSONObject item = new JSONObject();
            String name = ((Element) o).attr("data-name");
            String href = ((Element) o).attr("onclick");
            href = href.replace("lifedetail(", "").replace(")", "");
            String [] hrefArr = href.split(",");

            item.put("name", name);
            item.put("url",
                    String.format("https://www.zjzwfw.gov.cn/zjservice/home/corporate/getItemDetail.do?regionId=%s&eventType=%s&id=%s&webId=%s&sy=%s"
                                    , hrefArr[0].replace("'", ""),
                            hrefArr[1].replace("'", ""),
                            hrefArr[2].replace("'", ""),
                            hrefArr[3].replace("'", ""),
                            hrefArr[4].replace("'", "")
                            ));
            out.add(item);
        }
        return out;
    }

    private static void getSubTopic(String url, RecordSender recordSender, List<Object> fieldsList, String region, String module){
        try {
            String response = getReq(url, UtilZheJiang.getHeaders(), new JSONObject());
            List<JSONObject> out = new ArrayList<>();
            JXDocument jxDocument = new JXDocument(response);
            List<Object> listObj = jxDocument.sel("//div[@class='subtitle_list']/div");
            for(Object o : listObj){
            String name = ((Element) o).child(0).text();
            Elements lis =  ((Element) o).child(1).children();
            for(Element li : lis){
                if(li.tagName().equals("li")){
                    String title = li.child(0).attr("title");
                    String href = li.child(0).attr("onclick");
                    href = href.replace("goHot(", "").replace(")", "");
                    String [] hrefArr = href.split(",");
                    JSONObject outItem = new JSONObject();
                    outItem.put("名称", name + "-" + title);
                    String detailUrl =  goHot(
                            hrefArr[0].replace("'", ""),
                            hrefArr[1].replace("'", ""),
                            hrefArr[2].replace("'", ""),
                            hrefArr[3].replace("'", "")
                    );
                    outItem.put("地址", detailUrl);
                    processItem(
                            processResult(
                                    url, response, detailUrl, "",
                                    outItem, region, module + "-" + name + "-" + title ),
                            recordSender,
                            fieldsList
                    );
                }
            }
            break;
        }
        }catch (Exception e){
            e.printStackTrace();
        }

    }

    private static List<JSONObject> getRegionList(){
        String url = "https://zjjcmspublic.oss-cn-hangzhou-zwynet-d01-a.internet.cloud.zj.gov.cn/jcms_files/jcms1/web1/site/script/zjzwfw-2019/new_json2.js";
        JSONObject headers = new JSONObject();
        headers.put("Host", "zjjcmspublic.oss-cn-hangzhou-zwynet-d01-a.internet.cloud.zj.gov.cn");
        headers.put("Referer", "https://www.zjzwfw.gov.cn/");
        headers.put("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36");
        String response = null;
        try {
            String responseOrg = getReq(url, headers, UtilZheJiang.getParams());
            response = new String(responseOrg.getBytes("iso-8859-1"), "utf-8");
        }catch (Exception e){
            e.printStackTrace();
        }
        response = response.replace("var jsonwebsite=", "");
        JSONArray dataList =  ( (JSONObject) JSONObject.parseArray(response).get(0)).getJSONArray("childs");
        List<JSONObject> out = new ArrayList<>();
        // 遍历市
        for(Object o : dataList){
            JSONObject shiItem = (JSONObject) o;
            // 遍历区
            JSONArray childs = shiItem.getJSONArray("childs");
            for(Object oo : childs){
                JSONObject quItem = (JSONObject) oo;
                JSONObject outItem = new JSONObject();
                outItem.put("code", quItem.getString("code"));
                outItem.put("id", quItem.getString("id"));
                outItem.put("name", shiItem.getString("name") + "-" + quItem.getString("name"));
                out.add(outItem);
            }
        }
        return out;
    }

    private static String goHot(String sxeventType, String eventid, String eventType, String webid) {

        if (sxeventType.equals("1")) {
            return "";
        } else {
            String url = String.format("https://www.zjzwfw.gov.cn/zjservice/item/sjdh/dealHot.do?1=1&eventType=%s&eventid=%s&webId=%s",
                    eventType, eventid, webid );
            String response = getReq(url, UtilZheJiang.getHeaders(), new JSONObject());
            JSONObject data = JSONObject.parseObject(response);
            if(data.getBoolean("success")){
                return String.format("https://www.zjzwfw.gov.cn/zjservice/item/detail/index.do?impleCode=%s&webId=%s",
                        data.getJSONObject("params").getString("impleCode"),
                        webid
                );

            }else {
                return "";
            }
        }
    }

    public static void main(String[] args) {
        SpiderZheJiang spiderZheJiang = new SpiderZheJiang();
        spiderZheJiang.getData(null, new ArrayList<>());
    }
}
